﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sector7.Medical.Utility
{
    /// <summary>
    /// Citizen Util
    /// </summary>
    public class CitizenUtil
    {
        /// <summary>
        /// 计算年龄
        /// </summary>
        /// <param name="dateTime">出生日期</param>
        /// <returns></returns>
        public static string CalculateAge(DateTime birthDay)
        {
            string strAge = string.Empty;
            int intYear = 0;
            int intMonth = 0;
            int intDay = 0;

            DateTime dtNow = DateTime.Now;

            if (dtNow.CompareTo(birthDay) >= 0)
            {
                // 计算天数
                intDay = dtNow.Day - birthDay.Day;
                if (intDay < 0)
                {
                    dtNow = dtNow.AddMonths(-1);
                    intDay += DateTime.DaysInMonth(dtNow.Year, dtNow.Month);
                }

                // 计算月数
                intMonth = dtNow.Month - birthDay.Month;
                if (intMonth < 0)
                {
                    intMonth += 12;
                    dtNow = dtNow.AddYears(-1);
                }

                // 计算年数
                intYear = dtNow.Year - birthDay.Year;

                // 格式化年龄输出
                strAge = intYear.ToString() + "岁";

                if (intYear <= 6)
                {
                    strAge += intMonth.ToString() + "月";
                    strAge += intDay.ToString() + "天";
                }
            }

            return strAge;
        }
    }
}
